package com.xc.freeapp.account.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.CheckUtils;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.entity.Takemedic;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.TakemedicService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.DateUtils;
import com.xc.freeapp.util.TokenAnnotation;
import com.xc.freeapp.util.UploadUtil;

@Controller
@RequestMapping("/takemedic")
@Api(value="/takemedic" ,description = "定时吃药")
public class TakeMedicController extends AbstractController {
	@Autowired
	private TakemedicService takemedicservice;
	/**
	 * 获取定时吃药列表
	 * */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取定时吃药列表")
	public List<Takemedic> getTakeMedicList()
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());

		log.info("获取id" + authInfo.getUserId() + "的定时吃药列表");
		PageBounds pageBounds = new PageBounds(0, 100);
		List<Takemedic> takemediclist = takemedicservice
				.gettakemediclist(authInfo.getUserId(),pageBounds);
	    for (Takemedic takemedic : takemediclist) {
	    	if(takemedic.getTaketime()!=null&&!"".equals(takemedic.getTaketime())){
	    		List<String> taketimelist = JSONArray.parseArray(takemedic.getTaketime(), String.class);
		    	List<Integer> taketimeintlist=new ArrayList<Integer>();
		    	for (String string : taketimelist) {
					String [] a= string.split(":");
					taketimeintlist.add(Integer.valueOf(a[0])*3600*1000+Integer.valueOf(a[1])*60*1000);
				}
		    	takemedic.setTaketimelist(taketimeintlist);
		    	
	    	}
	    	
	    	takemedic.setPicslist(UploadUtil.pathstoList(takemedic.getPics()));
		}
		return takemediclist;
	}

	/**
	 * 定时吃药保存
	 * 
	 * @param datamap
	 * @return
	 * @throws BaseException
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.POST)
	 @ResponseBody
	 @ApiOperation(value = "保存定时吃药")
	public Takemedic saveTakeMedic(@RequestBody @Valid Takemedic takemedic) throws BaseException {
		CheckUtils.notNull(Constant.VER_NOTNULL, takemedic.getTaketime());
		
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		takemedic.setUserid(authInfo.getUserIntId());
		takemedic.setCreatetime(DateUtils.getDate());
		takemedic.setUpdatetime(DateUtils.getDate());
		takemedic.setCreateby(authInfo.getUserIntId());
		takemedic.setUpdateby(authInfo.getUserIntId());
		takemedic.setDelflag('t');
		 takemedic.setPics(UploadUtil.uploads(takemedic.getPicslist(),authInfo.getAppIntId()));
		takemedicservice.inserttakemedic(takemedic);
		Takemedic takemedics = takemedicservice.gettakemedic(takemedic.getId());
		takemedics.setPicslist(UploadUtil.pathstoList(takemedics.getPics()));
		return takemedics;
	}
	/**
	 * 更新定时吃药
	 * 
	 * @param datamap
	 * @return
	 * @throws BaseException
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.PUT)
	 @ResponseBody
	 @ApiOperation(value = "更新定时吃药")
	public Takemedic updateTakeMedic(@RequestBody Takemedic takemedic) throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		takemedic.setUserid(authInfo.getUserIntId());
		 takemedic.setPics(UploadUtil.uploads(takemedic.getPicslist(),authInfo.getAppIntId()));
		takemedicservice.updatetakemedic(takemedic);
		Takemedic takemedics = takemedicservice.gettakemedic(takemedic.getId());
		takemedics.setPicslist(UploadUtil.pathstoList(takemedics.getPics()));
		return takemedics;
	}
	

	/**
	 * 删除定时吃药
	 * 
	 * @throws BaseException
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.DELETE)
	@ResponseBody
	@ApiOperation(value = "删除定时吃药")
	public ResponseBean delTakeMedic(@RequestParam("id") String id)
			throws BaseException {
		takemedicservice.deletetakemedic(id);
		return ResponseBean.getSuccess();
	}


}
